package com.ruoyi.wisdom.event;

import com.ruoyi.wisdom.domain.IotStudentInfo;
import com.ruoyi.wisdom.domain.swap.SwapStudentInfo;
import com.ruoyi.wisdom.service.IIotStudentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class StudentInfoEvent extends IotEvent{

    @Autowired
    private IIotStudentInfoService iotStudentInfoService;

    /**
     * 处理
     * @param swapStudentInfo
     */
    public void handle(SwapStudentInfo swapStudentInfo) {
        if (swapStudentInfo.getOperate() == null) {
            save(swapStudentInfo);
        } else {
            switch (swapStudentInfo.getOperate()) {
                case "delete":
                case "remove":
                    delete(swapStudentInfo);
                    break;
                default:
                    save(swapStudentInfo);
            }
        }
    }

    /**
     * 添加|编辑
     * @param swapStudentInfo
     */
    public void save(SwapStudentInfo swapStudentInfo)
    {
        IotStudentInfo result = iotStudentInfoService.selectIotStudentInfoById(swapStudentInfo.getId());
        // 实体装换
        IotStudentInfo iotStudentInfo = getModelMapper().map(swapStudentInfo, IotStudentInfo.class);
        if (result != null) {
            iotStudentInfo.setId(result.getId());
            iotStudentInfoService.updateIotStudentInfo(iotStudentInfo);
        } else {
            iotStudentInfoService.insertIotStudentInfo(iotStudentInfo);
        }
    }

    /**
     * 删除
     * @param swapStudentInfo
     */
    public void delete(SwapStudentInfo swapStudentInfo){
        iotStudentInfoService.deleteIotStudentInfoById(swapStudentInfo.getId());
    }
}
